using System.Collections.Generic;
using Sixeyed.CodeGeneration.Metadata.Interfaces;

namespace Sixeyed.CodeGeneration.Metadata.Bases
{
    /// <summary>
    /// Abstract base class for metadata source
    /// </summary>
    public abstract class SourceBase : IMetadataSource
    {
        #region Abstract members

        /// <summary>
        /// Name of this source type
        /// </summary>
        public abstract string SourceTypeName {get;}

        /// <summary>
        /// Metadata source name
        /// </summary>
        public abstract string Name { get;}

        /// <summary>
        /// Metadata source description
        /// </summary>
        public abstract string Description { get; }

        #endregion

        #region Private instance fields

        private string _uri;
        private Dictionary<string, IMetadataProvider> _providers;

        #endregion

        #region Instance properties

        /// <summary>
        /// Display name for the Uri of the source
        /// </summary>
        public virtual string UriDisplayName
        {
            get { return "Uri"; }
        }
        
        /// <summary>
        /// Uri of the source
        /// </summary>
        public virtual string Uri 
        { 
            get{ return this._uri; }
            set { this._uri = value; }
        }

        /// <summary>
        /// Collection of metadata providers, keyed by name
        /// </summary>
        protected Dictionary<string, IMetadataProvider> Providers
        {
            get
            {
                if (this._providers == null)
                {
                    this._providers = new Dictionary<string, IMetadataProvider>();
                }
                return this._providers;
            }
        }

        #endregion

        #region Instance methods

        /// <summary>
        /// Adds a new provider of the given type
        /// </summary>
        /// <typeparam name="TProvider">IMetadataProvider type</typeparam>
        protected virtual void LoadProvider<TProvider>()
            where TProvider : IMetadataProvider, new()
        {
            IMetadataProvider provider = new TProvider();
            this.AddProvider(provider);

        }

        /// <summary>
        /// Returns a list of all metadata provider names
        /// </summary>
        /// <returns>List of provider names</returns>
        public List<string> GetProviderNames()
        {
            List<string> providerNames = new List<string>(this.Providers.Count);
            foreach (IMetadataProvider provider in this.Providers.Values)
            {
                providerNames.Add(provider.Name);
            }
            return providerNames;
        }

        /// <summary>
        /// Returns the metadata provider with the given name
        /// </summary>
        /// <param name="providerName">Provider name</param>
        /// <returns>IMetadataProvider</returns>
        public IMetadataProvider GetProvider(string providerName)
        {
            IMetadataProvider provider = null;
            if (this.Providers.ContainsKey(providerName))
            {
                provider = this.Providers[providerName];
            }
            return provider;
        }

        /// <summary>
        /// Adds the given provider to the source's collection
        /// </summary>
        /// <param name="provider">IMetadataProvider</param>
        public void AddProvider(IMetadataProvider provider)
        {
            provider.Source = this;
            this.Providers.Add(provider.Name, provider);
        }

        /// <summary>
        /// Loads all metadata providers for the source
        /// </summary>
        /// <param name="providers">All available providers</param>
        public void LoadProviders(List<IMetadataProvider> providers)
        {
            foreach (IMetadataProvider provider in providers)
            {
                if (provider.SourceTypeName == this.SourceTypeName &&
                    (string.IsNullOrEmpty(provider.SourceName) || provider.SourceName == this.Name))
                {
                    this.AddProvider(provider);
                }
            }
        }

        #endregion
    }
}
